package keyboard;

import java.util.HashMap;
import java.util.HashSet;

class GroupCostCalculatorUsingArrowKey extends GroupCostCalculator {
	public GroupCostCalculatorUsingArrowKey(Keyboard keyboard, HashSet<Character> keysGroup, HashMap<Character, Float> keysValue, Position lastPosition) {
		super(keyboard, keysGroup, keysValue, lastPosition);
	}

	@Override
	public float GetTheCostOfPriority(int priority) {
		int pos = (priority - 1) % 5 + 1;
		int page = (priority - 1) / 5 + 1;
		return 1 + (page - 1) * 3 + (pos / 2);
	}
	
	@Override
	public float GetExpectedCost() {
		float coverValue = 0f;
		for (int r = 0; r < Keyboard.layout.length; r++)
			for (int c = 0; c < Keyboard.layout[0].length; c++) {
				if (keysGroup.contains(Keyboard.layout[r][c]))
					continue;
				int nowR = (int)lastPosition.r, nowC = (int)lastPosition.c;
				while (true) {
					if (keysGroup.contains(Keyboard.layout[nowR][nowC])) {
						coverValue += keysValue.get(Keyboard.layout[r][c]);
						break;
					}
					if (nowR > r) nowR--; else
					if (nowR < r) nowR++; else
					if (nowC > c) nowC--; else
					if (nowC < c) nowC++; else break;
				}
			}
		
		return coverValue + GetSelectingCost();
	}
}
